package com.xhu.dao.impl;

import com.xhu.bean.pojo.Analyse;
import com.xhu.dao.AnalyseDao;
import com.xhu.utils.ConnectionFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AnalyseDaoImpl implements AnalyseDao {

    @Override
    public List<Analyse> findAll() {
        String  sql="select analyse_ID,analyse_Count" +
                "   from  analyse order by analyse_Month desc";
        //获得连接对象
        Connection   connection=ConnectionFactory.openConnection();
        //执行对象
        QueryRunner  queryRunner=new QueryRunner();
        //执行获得结果集
        List<Analyse>  list=null;
        try {
            list=queryRunner.
                    query(connection,sql,
                            new BeanListHandler<>(Analyse.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection(connection);
        }

        return list;
    }

    @Override
    public List<Analyse> querPage(Integer start, Integer limit, String name) {
        List<Object>   parms=new ArrayList<>();
        StringBuffer   sql=new StringBuffer("select  * from  analyse ");
        if(name!=null){
            parms.add("%"+name+"%");
        }
        sql.append( " order by analyse_Count desc  limit   ?,?" );
        parms.add(start);
        parms.add(limit);
        //获得连接对象
        Connection   connection=ConnectionFactory.openConnection();
        //执行对象
        QueryRunner  queryRunner=new QueryRunner();
        //执行获得结果集
        List<Analyse>  list=null;
        try {
            list=queryRunner.
                    query(connection,sql.toString(),parms.toArray(),
                            new BeanListHandler<>(Analyse.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection(connection);
        }

        return list;
    }
}
